package com.price.sample.ui

import android.content.Context
import android.os.Bundle
import android.view.View.OnClickListener
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.price.common.base.BaseActivity
import com.price.common.extend.addStatusBarHeightToPaddingTop
import com.price.common.extend.attachLanguage
import com.price.common.extend.pressBackTwiceToExitApp
import com.price.common.extend.startActivity
import com.price.location.LocationClient
import com.price.sample.SimpleAdapter
import com.price.sample.http.HttpActivity
import com.price.util.TelephonyUtils
import com.zp.common.R
import kotlin.jvm.Throws

class MainActivity : BaseActivity() {


    private val items = listOf(
        R.string.dialogs, R.string.spannable_string, R.string.activity_result,
        R.string.http_demo, R.string.keep_alive, R.string.view, R.string.nfc,R.string.socket, R.string.other
    )
    private val adapter = SimpleAdapter()

    override fun getViewId(): Int =R.layout.activity_recycler_view

    @Throws(Exception::class)
    override fun initView(savedInstanceState: Bundle?) {

        val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
        recyclerView.addStatusBarHeightToPaddingTop()
        recyclerView.adapter = adapter
        adapter.submitList(items)

        adapter.doOnClick = OnClickListener {
            when (it.tag as Int) {
                R.string.dialogs -> startActivity<DialogsActivity>()
                R.string.spannable_string -> startActivity<SpannableStringActivity>()
                R.string.activity_result -> startActivity<ActivityResultActivity>()
                R.string.http_demo -> startActivity<HttpActivity>()
                R.string.keep_alive -> startActivity<KeepAliveSettingActivity>()
                R.string.view -> startActivity<ViewActivity>()
                R.string.nfc -> startActivity<NFCActivity>()
                R.string.socket -> startActivity<SocketActivity>()
                R.string.other -> {
//                    ExcelUtil.demo(this)
//                    snackbar("test")
                    startActivity<OtherActivity>()
                }
            }
        }

        pressBackTwiceToExitApp(R.string.exit_app)
    }

    private lateinit var tvMsg:TextView
    private var locationClient:LocationClient?=null
    override fun initData(savedInstanceState: Bundle?) {
        tvMsg=findViewById(R.id.tv_msg)
        //初始化实例
//        locationClient = LocationClient(this)
//
//        //可根据具体需求设置定位配置参数
//        val locationOption = locationClient.getLocationOption()
//            .setLastKnownLocation(false)
//            .setProvider(LocationManager.NETWORK_PROVIDER)
//            .setAccuracy(Criteria.ACCURACY_FINE)//设置位置精度：高精度
//            .setMinTime(10000)//设置位置更新最小时间间隔（单位：毫秒）； 默认间隔：10000毫秒，最小间隔：1000毫秒
//            .setOnceLocation(false)//设置是否只定位一次，默认为 false，当设置为 true 时，则只定位一次后，会自动停止定位
//        //设置定位配置参数
//        locationClient.setLocationOption(locationOption)
//
//        //----------------------------------
//
//        //设置定位监听
//        locationClient.setOnLocationListener(object: OnLocationListener(){
//            override fun onLocationChanged(location: Location) {
//                logError("onLocationChanged:${location}")
//                //TODO 位置信息
//                tvMsg.text= buildString {
//                    append("经纬度：${location.longitude}:${location.latitude}")
//                }
//            }
//        })
//
//        //----------------------------------
//        if (isPermissionGranted(Manifest.permission.ACCESS_FINE_LOCATION) && isPermissionGranted(Manifest.permission.ACCESS_COARSE_LOCATION)) {
//            //开始定位（建议先校验是否有定位权限，然后开始定位）
//            locationClient.startLocation()
//
//            getTelephoney()
//        }else{
//            requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION),11)
//        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        locationClient?.startLocation()
        getTelephoney()
    }

    private fun getTelephoney(){
        val tData=TelephonyUtils.getData(this)

        tvMsg.text= buildString {
            appendLine("cid:${tData.cid} lac:${tData.lac}")
            append("mcc:${tData.mcc} mnc:${tData.mnc}")
        }

    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase?.attachLanguage())
    }

    override fun onDestroy() {
        locationClient?.stopLocation()
        super.onDestroy()
    }
}